﻿using ProjScan.RecordManager.AssetData;
using ProjScan.Tools;
using System;
using System.Collections.Generic;
using System.IO;
using UnityEditor.SceneManagement;
using UnityEngine;

namespace ProjScan.RecordManager
{
    internal class HierarchObjectRecordManager : RecordManagerBase<HierarchObjectAssetData>
    {
        public override string TableTag => "SceneComponentReference";

        public override void PrepareAssets()
        {
            Dictionary<string, int> dictionary = new Dictionary<string, int>();
            dictionary.Clear();
            string[] files = Directory.GetFiles(Application.dataPath, "*", SearchOption.AllDirectories);
            List<string> list = new List<string>();
            for (int i = 0; i < files.Length; i++)
            {
                string text = ToolsHelper.ConvertAbsolutePathToRelative(files[i]);
                if (text.EndsWith(".unity") && !dictionary.ContainsKey(text) && !dictionary.ContainsKey(text))
                {
                    list.Add(text);
                    dictionary.Add(text, dictionary.Count);
                }
            }

            int num = AssetPathManager.Instance.artPathList.Length;
            datas = new List<HierarchObjectAssetData>();
            for (int j = 0; j < num; j++)
            {
                string text2 = list[j];
                try
                {
                    EditorSceneManager.OpenScene(text2);
                }
                catch (Exception ex)
                {
                    Debug.LogError("Could not open scene at path \"" + text2 + "\". This scene was added to the build, and it's possible that it has been deleted: Error: " + ex.Message);
                    continue;
                }

                ActiveAllRecord();
                if (!CheckAssetPathRules(text2))
                {
                    continue;
                }

                GameObject[] array = UnityEngine.Object.FindObjectsOfType<GameObject>();
                int iD = AssetPathManager.Instance.GetID(Path.GetDirectoryName(text2));
                GameObject[] array2 = array;
                foreach (GameObject gameObject in array2)
                {
                    string transformHierarchy = GetTransformHierarchy(gameObject);
                    Component[] components = gameObject.GetComponents<Component>();
                    foreach (Component assetComponent in components)
                    {
                        HierarchObjectAssetData hierarchObjectAssetData = new HierarchObjectAssetData(iD, assetComponent, new List<string>());
                        hierarchObjectAssetData.hierarchy = transformHierarchy;
                        hierarchObjectAssetData.id = datas.Count;
                        ValidateAsset(hierarchObjectAssetData);
                        datas.Add(hierarchObjectAssetData);
                        if (datas.Count % 100 == 0)
                        {
                            ToolsHelper.GC();
                        }
                    }
                }
            }
        }

        private static string GetTransformHierarchy(GameObject gameObject)
        {
            Transform parent = gameObject.transform.parent;
            return (parent == null) ? gameObject.name : (GetTransformHierarchy(parent.gameObject) + "/" + gameObject.name);
        }
    }
}